/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package aso.upna.controllers;

import aso.upna.filesystem.FileIterator;
import aso.upna.filesystem.SharedFolder;
import java.io.File;
import javax.swing.JTable;

/**
 *
 * @author triki
 */
public class SearchTableController {

    private JTable table;
    private SharedFolder local_list;
    private boolean show_local;
    private boolean show_remote;

    public SearchTableController(JTable jTable)
    {
        table = jTable;
    }

    public void registerLocalList(SharedFolder local)
    {
        local_list = local;
    }

    public void setShowLocal(boolean local)
    {
        show_local = local;
    }


    public void refresh()
    {
        int rows,cols,i,j;
        FileIterator iter = null;
        File aux = null;
        String sAux = null;
        
        rows = table.getRowCount();
        cols = table.getColumnCount();

        cleanTable();

        if(show_local)
        {
            iter = local_list.getIterator();
            
            i=0;
            j=0;

            local_list.listFiles();

            while(iter.hasNext())
            {
                sAux = iter.current();
                aux = new File(local_list.getPath()+sAux);

                table.setValueAt("Local",i, 0);
                table.setValueAt(" - ",i, 1);
                table.setValueAt(sAux,i, 2);
                table.setValueAt(""+aux.length()+" bytes", i, 4);

                iter.next();
                i++;
            }
        }
    }

    private void cleanTable()
    {
        int rows,cols,i,j;
        rows = table.getRowCount();
        cols = table.getColumnCount();

        for(i=0;i<rows;i++)
        {
            for(j=0;j<cols;j++)
            {
                table.setValueAt("", i, j);
            }
        }
    }
}
